<?php
namespace app\util;

use think\facade\Log;

class BEIBEIGFUtil {

    /**
     * 登录
     */
    public static function login(){
        $url = 'https://em.hztend.com/platform/j_spring_security_check';
        $data = 'j_username='.env('BEIBEI.BEIBEI_USERNAME').'&j_password='.env('BEIBEI.BEIBEI_PASSWORD');
        $res = RequestUtil::post($url, $data);
        if (!isset($res['code']) || $res['code'] != '0') {
            Log::error('登录接口调用失败 -> '.$url.$data);
            Log::error($res);
            return null;
        }
//        Log::error('贝贝管房 token = '.$res['token']);
        return 'JSESSIONID='.$res['token'];
    }

    /**
     * 获取设备列表
     * @param $bar_measure_no 表号
     */
    public static function getMeterData(string $bar_measure_no){
        $url = 'https://em.hztend.com/platform/bar/mc/getMeterData?bar_project_id='.env('BEIBEI.BEIBEI_PROJECT_ID');
        if ($bar_measure_no) {
            $url = $url.'&bar_measure_no='.$bar_measure_no;
        }
        $res = RequestUtil::get($url, array('Cookie: '.BEIBEIGFUtil::login()));
        if (!isset($res['code']) || $res['code'] != '0') {
            Log::error('获取设备列表接口调用失败 -> '.$url);
            Log::error($res);
            return null;
        }
        if (!isset($res['data']['totalCount']) || $res['data']['totalCount'] == 0) {
            Log::error('获取设备列表接口调用失败,找不到设备信息 -> '.$url);
            return null;
        }
        return $res['data']['result'];
    }

    /**
     * 设备充值
     * @param $bar_measure_no 表号
     * @param $money 充值金额(元/单位)
     */
    public static function consumerCashRecharge(string $bar_measure_no, string $money){
        $result = self::getMeterData($bar_measure_no);
        if (!$result || count($result) == 0) {
            $res = [];
            $res['code'] = '1';
            $res['msg'] = '找不到设备号对应的设备 '.$bar_measure_no;
            return $res;
        }
        $bar_measure_id = $result[0]['id'];
        $url = 'https://em.hztend.com/platform/bar/mc/consumerCashRecharge';
        $data = [
            'bar_project_id'=>env('BEIBEI.BEIBEI_PROJECT_ID'),
            'bar_measure_id'=>$bar_measure_id,
            'money'=>$money,
            'pay_way'=>0,
            'bluetooth'=>2
        ];
        $res = RequestUtil::post($url ,$data, true, array('Cookie: '.BEIBEIGFUtil::login()));
        if ($res['code'] != '0') {
            Log::error('设备充值接口调用失败 -> '.$url);
            Log::error($data);
            Log::error($res);
            return $res;
        }
        return $res['data'];
    }

    /**
     * 充值补发
     * @param $serialNo 渠道订单号
     */
    public static function rechargeRetry(string $serialNo){
        $url = 'https://em.hztend.com/platform/bar/mc/rechargeRetry';
        $data = [
            'serialNo'=>$serialNo
        ];
        $res = RequestUtil::post($url ,$data, false, array('Cookie: '.BEIBEIGFUtil::login()));
        if (!isset($res['code']) || $res['code'] != '0') {
            Log::error('充值补发接口调用失败 -> '.$url);
            Log::error($data);
            Log::error($res);
            return $res;
        }
        return $res;
    }

    /**
     * 获取月账单
     */
    public static function getConsConsumeHistoryRecord(string $bar_measure_id, string $bar_measure_type, string $date){
        $url = 'https://em.hztend.com/platform/bar/mc/program/getConsConsumeHistoryRecord';
        $data = [
            'bar_project_id'=>env('BEIBEI.BEIBEI_PROJECT_ID'),
            'bar_measure_id'=>$bar_measure_id,
            'bar_measure_type'=>$bar_measure_type,
            'date'=>$date
        ];
        $res = RequestUtil::post($url ,$data, true, array('Cookie: '.BEIBEIGFUtil::login()));
        if ($res['code'] != '0') {
            Log::error('获取月账单接口调用失败 -> '.$url);
            Log::error($data);
            Log::error($res);
            return $res;
        }
        return $res['data'];
    }

}
?>